//
//  AsynDownImage.m
//  YOKAShopShow
//
//  Created by 王思源ibokan on 14-5-16.
//  Copyright (c) 2014年 ibokan. All rights reserved.
//

#import "AsynDownImage.h"

@interface AsynDownImage ()

@end

@implementation AsynDownImage

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
// *** 根据图片的地址异步下载图片
-(void)getUrlToDownImage:(NSURL *)url{
    if (url!=0) {
        self.downData=[[NSMutableData alloc]init];
    }
    NSURLRequest *req=[NSURLRequest requestWithURL:url];
    [NSURLConnection connectionWithRequest:req delegate:self];
}



#pragma mark -NSURLConnectionDelegate
//连接开始了
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
}
//连接接收数据会被多次执行
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [self.downData appendData:data];
}
//连接结束
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    self.image=[UIImage imageWithData:self.downData];
    // 下载好了 创建一个通知进行通知主界面我已经下载好了;
    NSNotification *notification=nil;
    notification =[NSNotification notificationWithName:@"image" object:nil userInfo:[NSDictionary dictionaryWithObject:self.image forKey:@"downImage"]];
    [[NSNotificationCenter defaultCenter]postNotification:notification];
}

@end
